package com.study.fileCheck;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CheckService {
    // 认为readerA读取关联方系统文件,readerB读取平台流水文件
    SortedFileReader readerA, readerB;
    int key; // 勾兑参考流水值的索引

    //一对一勾兑
    private void checkRecords() throws Exception {
        String[] arrayA = readerA.read(); // 记录行, 已经通过分隔符进行切分的字段
        String[] arrayB = readerB.read();

        while (arrayA != null && arrayB != null) {
            int k = arrayA[key].compareTo(arrayB[key]);
            // 勾上
            if (k == 0) {
                // 对比要素
                deal1(arrayA, arrayB);
                arrayA = readerA.read();
                arrayB = readerB.read();
            } else if (k < 0) { // A多了记录
                deal2(arrayA);
                arrayA = readerA.read();
            } else { // B多了记录
                deal3(arrayB);
                arrayB = readerB.read();
            }
        }

        if (arrayA != null) {
            do {
                deal2(arrayA);
                arrayA = readerA.read();
            } while (arrayA != null);
        } else if (arrayB != null) {
            do {
                deal3(arrayB);
                arrayB = readerB.read();
            } while (arrayB != null);
        }
    }

    private void deal3(String[] arrayB) {
    }

    private void deal2(String[] arrayA) {

    }

    private void deal1(String[] arrayA, String[] arrayB) {

    }


    //一对多勾兑
    private void checkRecords2() throws Exception {
        String[] arrayA = readerA.read(); // 记录行, 已经通过分隔符进行切分的字段
        String[] arrayB = readerB.read();

        List<String[]> arrays = new ArrayList<>();
        while (arrayA != null && arrayB != null) {
            int k = arrayA[key].compareTo(arrayB[key]);
            if (k == 0) {
                arrays.add(arrayB);
                arrayB = readerB.read();
                if (arrayB != null)
                    continue;
            }
            if (!arrays.isEmpty()) {
                deal4(arrayA, arrays);
                arrays.clear();
                arrayA = readerA.read();
                continue;
            }

            if (k < 0) {
                deal2(arrayA);
                arrayA = readerA.read();
            } else {
                List<String[]> _arrays = new ArrayList<>();
                String _key = arrayB[key];
                do {
                    _arrays.add(arrayB);
                    arrayB = readerB.read();
                } while (arrayB != null && _key.equals(arrayB[key]));
                deal5(_arrays);
            }
        }

        // 已经有一方勾完了,另一方剩余的数据应该都是多的
        if (arrayA != null) {
            do {
                deal2(arrayA);
                arrayA = readerA.read();
            } while (arrayA != null);
        } else if (arrayB != null) {
            Map<String, List<String[]>> map = new HashMap<>();
            do {
                List<String[]> list = map.get(arrayB[key]);
                if (list == null) {
                    list = new ArrayList<>();
                }
                list.add(arrayB);
                map.put(arrayB[key], list);

                arrayB = readerB.read();
            } while (arrayB != null);
            for (String _key : map.keySet()) {
                List<String[]> list = map.get(_key);
                deal6(list);
            }
        }
    }

    private void deal5(List<String[]> arrays) {
    }

    private void deal6(List<String[]> list) {
    }

    private void deal4(String[] arrayA, List<String[]> arrays) {
    }
}
